﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace FamilyTree_v1._0
{
    public partial class ConstructionGenealogy : UserControl
    {
        private Pen pen;
        private Color StringColor;
        private int LineHeight = 60;
        private int d = 22;
        private Graphics g;
        private bool sex = true;
        private Node person;
        private frmAddPerson frmAddPerson;
        private User_Control.UserMemberCard1 ucUserMemberCard1_Male;
        private User_Control.UserMemberCard1 ucUserMemberCard1_Female;
        private User_Control.UserMemberCard2 ucUserMemberCard2_Father;
        private User_Control.UserMemberCard2 ucUserMemberCard2_Mother;
        private User_Control.UserMemberCard3 ucUserMemberCard3_AddChild;
        private DevExpress.XtraEditors.LabelControl lblWifeList;
        private DevExpress.XtraEditors.LabelControl lblHusbandList;
        DatabaseHelper db;
        public ConstructionGenealogy()
        {
            InitializeComponent();
            db = new DatabaseHelper();
            ucUserMemberCard1_Male = new User_Control.UserMemberCard1();
            ucUserMemberCard1_Female = new User_Control.UserMemberCard1();
            ucUserMemberCard2_Father = new User_Control.UserMemberCard2();
            ucUserMemberCard2_Mother = new User_Control.UserMemberCard2();
            ucUserMemberCard3_AddChild = new User_Control.UserMemberCard3();
            lblWifeList = new DevExpress.XtraEditors.LabelControl();
            lblHusbandList = new DevExpress.XtraEditors.LabelControl();
            pen = new Pen(new SolidBrush(Color.Red));
            StringColor = Color.Red;
            g = this.CreateGraphics();
            person = new Node();
            lblHusbandList.Width = 150;
            lblWifeList.Width = 150;
            lblWifeList.Text = "Danh sách vợ";
            lblHusbandList.Text = "Danh sách chồng";
            lblWifeList.ForeColor = Color.Blue;
            lblHusbandList.ForeColor = Color.Blue;
            lblWifeList.Font = new System.Drawing.Font("Tahoma", 10, FontStyle.Underline);
            lblHusbandList.Font = new System.Drawing.Font("Tahoma", 10, FontStyle.Underline);
            ucUserMemberCard1_Male = new User_Control.UserMemberCard1();
            ucUserMemberCard1_Female = new User_Control.UserMemberCard1();
            ucUserMemberCard2_Father = new User_Control.UserMemberCard2();
            ucUserMemberCard2_Mother = new User_Control.UserMemberCard2();
            ucUserMemberCard3_AddChild = new User_Control.UserMemberCard3();
            ucUserMemberCard1_Male.function_EventChangeSex = new User_Control.UserMemberCard1.EventChangeSex(ChangeSex_Male);
            ucUserMemberCard1_Female.function_EventChangeSex = new User_Control.UserMemberCard1.EventChangeSex(ChangeSex_Female);
            ucUserMemberCard1_Male.function_EventAdd_Edit = new User_Control.UserMemberCard1.EventAdd_Edit(Add_Edit);
            ucUserMemberCard1_Female.function_EventAdd_Edit = new User_Control.UserMemberCard1.EventAdd_Edit(Add_Edit);
            ucUserMemberCard2_Father.function_EventAdd_Edit = new User_Control.UserMemberCard2.EventAdd_Edit(Add_Edit);
            ucUserMemberCard2_Mother.function_EventAdd_Edit = new User_Control.UserMemberCard2.EventAdd_Edit(Add_Edit);
            ucUserMemberCard1_Male.function_EventDelete = new User_Control.UserMemberCard1.EventDelete(Delete);
            ucUserMemberCard1_Female.function_EventDelete = new User_Control.UserMemberCard1.EventDelete(Delete);
            ucUserMemberCard1_Female.BackColor = Color.OrangeRed;
            ucUserMemberCard2_Mother.BackColor = Color.OrangeRed;
            Controls.Add(ucUserMemberCard1_Male);
            Controls.Add(ucUserMemberCard1_Female);
            Controls.Add(ucUserMemberCard2_Father);
            Controls.Add(ucUserMemberCard2_Mother);
            Controls.Add(lblHusbandList);
            Controls.Add(lblWifeList);
            Controls.Add(ucUserMemberCard3_AddChild);
            ResizePanel(Width, Height);
            showMemberInfor();
        }
        public void ResizePanel(int width, int height)
        {
            this.Width = width;
            Height = height;
            ucUserMemberCard1_Male.Location = new Point(this.Width / 2 - ucUserMemberCard1_Male.Width - d, ucUserMemberCard2_Father.Height + LineHeight);
            ucUserMemberCard1_Female.Location = new Point(this.Width / 2 + d, ucUserMemberCard2_Father.Height + LineHeight);
            ucUserMemberCard2_Father.Location = new Point(this.Width / 2 - ucUserMemberCard2_Father.Width - d, 0);
            ucUserMemberCard2_Mother.Location = new Point(this.Width / 2 + d, 0);
            lblWifeList.Location = new Point(this.Width / 2 + d, ucUserMemberCard1_Male.Location.Y + d + ucUserMemberCard1_Male.Height);
            lblHusbandList.Location = new Point(this.Width / 2 - d - lblHusbandList.Width, ucUserMemberCard1_Male.Location.Y + d + ucUserMemberCard1_Male.Height);
            ucUserMemberCard3_AddChild.Location = new Point(ucUserMemberCard1_Male.Location.X + (ucUserMemberCard1_Male.Width / 2 - ucUserMemberCard3_AddChild.Width / 2), ucUserMemberCard1_Male.Location.Y + ucUserMemberCard1_Male.Height + d);
            Invalidate();
        }
        private void ConstructionGenealogy_Paint(object sender, PaintEventArgs e)
        {
            g.Clear(this.BackColor);
            if (sex)
            {
                lblWifeList.Visible = true;
                lblHusbandList.Visible = false;
                g.DrawLine(pen, new Point(ucUserMemberCard2_Father.Location.X + ucUserMemberCard2_Father.Width / 2, ucUserMemberCard2_Father.Location.Y + ucUserMemberCard2_Father.Height), new Point(ucUserMemberCard2_Father.Location.X + ucUserMemberCard2_Father.Width / 2, ucUserMemberCard2_Father.Location.Y + ucUserMemberCard2_Father.Height + LineHeight / 4));
                g.DrawLine(pen, new Point(ucUserMemberCard2_Mother.Location.X + ucUserMemberCard2_Mother.Width / 2, ucUserMemberCard2_Mother.Location.Y + ucUserMemberCard2_Mother.Height), new Point(ucUserMemberCard2_Mother.Location.X + ucUserMemberCard2_Mother.Width / 2, ucUserMemberCard2_Mother.Location.Y + ucUserMemberCard2_Mother.Height + LineHeight / 4));
                g.DrawLine(pen, new Point(ucUserMemberCard2_Father.Location.X + ucUserMemberCard2_Father.Width / 2, ucUserMemberCard2_Father.Location.Y + ucUserMemberCard2_Father.Height + LineHeight / 4), new Point(ucUserMemberCard1_Female.Location.X + ucUserMemberCard2_Mother.Width / 2, ucUserMemberCard2_Mother.Height + LineHeight / 4));
                g.DrawLine(pen, new Point(this.Width / 2, ucUserMemberCard2_Father.Location.Y + ucUserMemberCard2_Father.Height + LineHeight / 4), new Point(this.Width / 2, ucUserMemberCard2_Father.Location.Y + ucUserMemberCard2_Father.Height + LineHeight / 2));
                g.DrawLine(pen, new Point(ucUserMemberCard1_Male.Location.X + ucUserMemberCard1_Male.Width / 2, ucUserMemberCard1_Male.Location.Y), new Point(ucUserMemberCard1_Male.Location.X + ucUserMemberCard1_Male.Width / 2, ucUserMemberCard1_Male.Location.Y - LineHeight / 2));
                g.DrawLine(pen, new Point(this.Width / 2, ucUserMemberCard2_Father.Location.Y + ucUserMemberCard2_Father.Height + LineHeight / 2), new Point(ucUserMemberCard1_Male.Location.X + ucUserMemberCard1_Male.Width / 2, ucUserMemberCard1_Male.Location.Y - LineHeight / 2));
                g.DrawLine(pen, new Point(this.Width / 2 - d, ucUserMemberCard1_Male.Location.Y + ucUserMemberCard1_Male.Height / 2), new Point(this.Width / 2 + d, ucUserMemberCard1_Female.Location.Y + ucUserMemberCard1_Female.Height / 2));
                g.DrawLine(pen, new Point(ucUserMemberCard1_Male.Location.X + ucUserMemberCard1_Male.Width / 2, ucUserMemberCard1_Male.Location.Y + ucUserMemberCard1_Male.Height), new Point(ucUserMemberCard1_Male.Location.X + ucUserMemberCard1_Male.Width / 2, ucUserMemberCard1_Male.Location.Y + ucUserMemberCard1_Male.Height + 2 * d));
                g.DrawString("Vợ", new Font("Arial", 10, FontStyle.Bold), new SolidBrush(StringColor), new PointF(this.Width / 2 - d, ucUserMemberCard1_Male.Location.Y + ucUserMemberCard1_Male.Height / 2 - 15));
                g.DrawString("Con", new Font("Arial", 10, FontStyle.Bold), new SolidBrush(StringColor), new PointF(this.Width / 2 - ucUserMemberCard1_Male.Width / 2 - d, ucUserMemberCard1_Male.Location.Y + ucUserMemberCard1_Male.Height + d / 2));
                ucUserMemberCard3_AddChild.Location = new Point(ucUserMemberCard1_Male.Location.X + (ucUserMemberCard1_Male.Width / 2 - ucUserMemberCard3_AddChild.Width / 2), ucUserMemberCard1_Male.Location.Y + ucUserMemberCard1_Male.Height + 2 * d);
            }
            else
            {
                lblHusbandList.Visible = true;
                lblWifeList.Visible = false;
                g.DrawLine(pen, new Point(ucUserMemberCard2_Father.Location.X + ucUserMemberCard2_Father.Width / 2, ucUserMemberCard2_Father.Location.Y + ucUserMemberCard2_Father.Height), new Point(ucUserMemberCard2_Father.Location.X + ucUserMemberCard2_Father.Width / 2, ucUserMemberCard2_Father.Location.Y + ucUserMemberCard2_Father.Height + LineHeight / 4));
                g.DrawLine(pen, new Point(ucUserMemberCard2_Mother.Location.X + ucUserMemberCard2_Mother.Width / 2, ucUserMemberCard2_Mother.Location.Y + ucUserMemberCard2_Mother.Height), new Point(ucUserMemberCard2_Mother.Location.X + ucUserMemberCard2_Mother.Width / 2, ucUserMemberCard2_Mother.Location.Y + ucUserMemberCard2_Mother.Height + LineHeight / 4));
                g.DrawLine(pen, new Point(ucUserMemberCard2_Father.Location.X + ucUserMemberCard2_Father.Width / 2, ucUserMemberCard2_Father.Location.Y + ucUserMemberCard2_Father.Height + LineHeight / 4), new Point(ucUserMemberCard1_Female.Location.X + ucUserMemberCard2_Mother.Width / 2, ucUserMemberCard2_Mother.Height + LineHeight / 4));
                g.DrawLine(pen, new Point(this.Width / 2, ucUserMemberCard2_Father.Location.Y + ucUserMemberCard2_Father.Height + LineHeight / 4), new Point(this.Width / 2, ucUserMemberCard2_Father.Location.Y + ucUserMemberCard2_Father.Height + LineHeight / 2));
                g.DrawLine(pen, new Point(ucUserMemberCard1_Female.Location.X + ucUserMemberCard1_Female.Width / 2, ucUserMemberCard1_Female.Location.Y), new Point(ucUserMemberCard1_Female.Location.X + ucUserMemberCard1_Female.Width / 2, ucUserMemberCard1_Female.Location.Y - LineHeight / 2));
                g.DrawLine(pen, new Point(this.Width / 2, ucUserMemberCard2_Father.Location.Y + ucUserMemberCard2_Father.Height + LineHeight / 2), new Point(ucUserMemberCard1_Female.Location.X + ucUserMemberCard1_Female.Width / 2, ucUserMemberCard1_Female.Location.Y - LineHeight / 2));
                g.DrawLine(pen, new Point(ucUserMemberCard1_Female.Location.X + ucUserMemberCard1_Female.Width / 2, ucUserMemberCard1_Female.Location.Y + ucUserMemberCard1_Female.Height), new Point(ucUserMemberCard1_Female.Location.X + ucUserMemberCard1_Female.Width / 2, ucUserMemberCard1_Female.Location.Y + ucUserMemberCard1_Female.Height + 2 * d));
                g.DrawLine(pen, new Point(this.Width / 2 - d, ucUserMemberCard1_Male.Location.Y + ucUserMemberCard1_Male.Height / 2), new Point(this.Width / 2 + d, ucUserMemberCard1_Female.Location.Y + ucUserMemberCard1_Female.Height / 2));
                g.DrawString("Chồng", new Font("Arial", 10, FontStyle.Bold), new SolidBrush(StringColor), new PointF(this.Width / 2 - d, ucUserMemberCard1_Male.Location.Y + ucUserMemberCard1_Male.Height / 2 - 15));
                g.DrawString("Con", new Font("Arial", 10, FontStyle.Bold), new SolidBrush(StringColor), new PointF(this.Width / 2 + ucUserMemberCard1_Female.Width / 2 - d / 2, ucUserMemberCard1_Female.Location.Y + ucUserMemberCard1_Female.Height + d / 2));
                ucUserMemberCard3_AddChild.Location = new Point(this.Width / 2 + d + (ucUserMemberCard1_Male.Width / 2 - ucUserMemberCard3_AddChild.Width / 2), ucUserMemberCard1_Female.Location.Y + ucUserMemberCard1_Female.Height + 2 * d);
            }
        }  
        private void Add_Edit(object sender, EventArgs e)
        {
            frmAddPerson = new frmAddPerson();
            DevExpress.XtraEditors.LabelControl lb = (DevExpress.XtraEditors.LabelControl)sender;
            if (lb.Parent.Name == "UserMemberCard1")
            {              
                User_Control.UserMemberCard1 uc1 = (User_Control.UserMemberCard1)lb.Parent;
                if(uc1.getSex())
                {
                    if (uc1.getEmpty())
                    {
                        if (sex)
                        {
                            frmAddPerson.function_EventSave = new frmAddPerson.EventSave(AddMember);
                            frmAddPerson.setMale();
                            frmAddPerson.ShowDialog();
                        }
                        else
                        {
                            frmAddPerson.function_EventSave = new frmAddPerson.EventSave(AddHusband);
                            frmAddPerson.setMale();
                            frmAddPerson.ShowDialog();
                        }
                    }
                    else
                    {
                        if (sex)
                        {
                            frmAddPerson.function_EventSave = new frmAddPerson.EventSave(EditMember);
                            frmAddPerson.setMale();
                            frmAddPerson.ShowDialog();
                        }
                        else
                        {
                            frmAddPerson.function_EventSave = new frmAddPerson.EventSave(EditHusband);
                            frmAddPerson.setMale();
                            frmAddPerson.ShowDialog();
                        }
                    }
                }
                else
                {
                    if (uc1.getEmpty())
                    {
                        if (!sex)
                        {
                            frmAddPerson.function_EventSave = new frmAddPerson.EventSave(AddMember);
                            frmAddPerson.setFemale();
                            frmAddPerson.ShowDialog();
                        }
                        else
                        {
                            frmAddPerson.function_EventSave = new frmAddPerson.EventSave(AddWife);
                            frmAddPerson.setFemale();
                            frmAddPerson.ShowDialog();
                        }
                    }
                    else
                    {
                        if (sex)
                        {
                            frmAddPerson.function_EventSave = new frmAddPerson.EventSave(EditMember);
                            frmAddPerson.setFemale();
                            frmAddPerson.ShowDialog();
                        }
                        else
                        {
                            frmAddPerson.function_EventSave = new frmAddPerson.EventSave(EditWife);
                            frmAddPerson.setFemale();
                            frmAddPerson.ShowDialog();
                        }
                    }
                }
            }
            if (lb.Parent.Name == "UserMemberCard2")
            {
                User_Control.UserMemberCard2 uc2 = (User_Control.UserMemberCard2)lb.Parent;
                if (uc2.getSex())
                {
                    if (uc2.getEmpty())
                    {
                        frmAddPerson.function_EventSave = new frmAddPerson.EventSave(AddFather);
                        frmAddPerson.setMale();
                        frmAddPerson.ShowDialog();
                    }
                    else
                    {
                        frmAddPerson.function_EventSave = new frmAddPerson.EventSave(EditFather);
                        frmAddPerson.setMale();
                        frmAddPerson.ShowDialog();
                    }
                }
                else
                {
                    if (uc2.getEmpty())
                    {
                        frmAddPerson.function_EventSave = new frmAddPerson.EventSave(AddMother);
                        frmAddPerson.setFemale();
                        frmAddPerson.ShowDialog();
                    }
                    else
                    {
                        frmAddPerson.function_EventSave = new frmAddPerson.EventSave(EditMother);
                        frmAddPerson.setFemale();
                        frmAddPerson.ShowDialog();
                    }
                }
            }
        }
        private void AddMember()
        {
            frmAddPerson.addMember();
            person.Data = frmAddPerson.getPersonInfor();
            frmAddPerson.Dispose();
            showMemberInfor();
        }
        private void AddFather()
        {
            MessageBox.Show("Add father");
          /*  frmAddPerson.addMember();
            person.ParentNode.Data=frmAddPerson.getPersonInfor();
            //Them moi quan he con cha
            Node father = new Node();          
            father = person.ParentNode;
            while (father.LeftNode != null)
            {
                person = person.LeftNode;
            }
            person.LeftNode = person;
            frmAddPerson.Dispose();
            showMemberInfor();*/
        }
        private void AddMother()
        {
       //     frmAddPerson.getInfor();
         //   person.ParentNode.Data = frmAddPerson.getPersonInfor();
            //Them moi quan he me con
          //  frmAddPerson.Dispose();
          //  showMemberInfor();
            MessageBox.Show("Add mother");
        }
        private void AddWife()
        {
            frmAddPerson.addMember();
            Person wife = new Person();
            wife=frmAddPerson.getPersonInfor();
            //Them moi quan he vo chong
            db.AddMarriage(person.Data.PersonID, wife.PersonID);
            frmAddPerson.Dispose();
            showMemberInfor();
        }
        private void AddHusband()
        {
            MessageBox.Show("Add husband");
        }
        private void AddChild()
        {
       //     frmAddPerson.getInfor();
         //   person.ParentNode.Data = frmAddPerson.getPersonInfor();
            //Them moi quan he cha con
            frmAddPerson.Dispose();
            showMemberInfor();
        }
        private void EditMember()
        {
            frmAddPerson.editMember();
            person.Data = frmAddPerson.getPersonInfor();
            frmAddPerson.Dispose();
            showMemberInfor();
        }
        private void EditFather()
        {

        }
        private void EditMother()
        {
        }
        private void EditWife()
        {
        }
        private void EditHusband()
        {
        }
        private void Delete()
        {
            if (person.Data!= null)
            {

            }
        }
        public void ChangeSex_Male()
        {
           
        }
        public void ChangeSex_Female()
        {
           
        }
        private void showMemberInfor()
        {
            if (this.person.Data == null)
            {
               ucUserMemberCard1_Male.setName("Thêm thành viên mới");
               ucUserMemberCard1_Male.setAvatar("Resources/male.png");
               ucUserMemberCard1_Male.setSex(true);
               ucUserMemberCard1_Male.setEmpty(true);
               ucUserMemberCard1_Female.setName("Thêm thành viên mới");
               ucUserMemberCard1_Female.setAvatar("Resources/female.png");
               ucUserMemberCard1_Female.setSex(false);
               ucUserMemberCard1_Female.setEmpty(true);
               ucUserMemberCard2_Father.setName("Thêm cha");
               ucUserMemberCard2_Mother.setName("Thêm mẹ");
               ucUserMemberCard2_Father.setAvatar("Resources/male.png");
               ucUserMemberCard2_Mother.setAvatar("Resources/female.png");
               ucUserMemberCard1_Female.Enabled = false;
               ucUserMemberCard2_Father.Enabled = false;
               ucUserMemberCard2_Mother.Enabled = false;
            }
            else
            {
                ucUserMemberCard1_Female.Enabled = true;
                ucUserMemberCard2_Father.Enabled = true;
                ucUserMemberCard2_Mother.Enabled = true;
                if (this.person.Data.Sex == true)
                {
                    ucUserMemberCard1_Male.setName(person.Data.FirstName + " "+ person.Data.MiddleName +" "+ person.Data.LastName);
                    ucUserMemberCard1_Male.setDateOfBirth(person.Data.DateOfBirth.ToString());
                    ucUserMemberCard1_Male.setPlaceOfBirth(person.Data.PlaceOfBirth);
                //    if(person.Data.Image.CompareTo(""))
                  //      ucUserMemberCard1_Male.setAvatar(person.Data.Image);                 
                    Marriage mr = new Marriage();
                    mr = db.SearchMarriageByID(person.Data.PersonID);
                    if (mr != null)
                    {
                        Person wife = new Person();
                        wife = db.SearchPersonByID(mr.FemaleID);
                        ucUserMemberCard1_Female.setName(wife.FirstName + " " + wife.MiddleName + " " + wife.LastName);
                    }
                }
                else
                {
                    ucUserMemberCard1_Female.setName(person.Data.FirstName + " " + person.Data.MiddleName + " " + person.Data.LastName);
                    ucUserMemberCard1_Female.setDateOfBirth(person.Data.DateOfBirth.ToString());
                    ucUserMemberCard1_Female.setPlaceOfBirth(person.Data.PlaceOfBirth);
                 //   if (person.Data.Image!="")
                //        ucUserMemberCard1_Female.setAvatar(person.Data.Image);
                    Marriage mr = new Marriage();
                    mr = db.SearchMarriageByID(person.Data.PersonID);
                    if (mr != null)
                    {
                        Person husband = new Person();
                        husband = db.SearchPersonByID(mr.FemaleID);
                        ucUserMemberCard1_Female.setName(husband.FirstName + " " + husband.MiddleName + " " + husband.LastName);
                    }
                }
                if (person.Data.IDofFather != null)
                {
                    ucUserMemberCard2_Father.setName(person.ParentNode.Data.FirstName + " " + person.ParentNode.Data.MiddleName + " " + person.ParentNode.Data.LastName);
                }
                if (person.Data.IDofMother != null)
                {
                    Person mother =new Person();
                    mother=db.SearchPersonByID(person.Data.IDofMother.Value);
                    ucUserMemberCard2_Mother.setName(mother.FirstName+" "+ mother.MiddleName+ " "+ mother.LastName);
                }
            }
        }
        public void ShowMember(Node person)
        {
            this.person = person;
            showMemberInfor();
        }
    }
}
